package br.unifacs.mda.plugin.code.generator;

import java.util.ArrayList;
import java.util.List;

import psmjava.JavaElement;
import psmjava.JavaProject;
import psmjava.impl.JavaClassImpl;
import psmjava.impl.JavaEnumerationImpl;
import psmjava.impl.JavaInterfaceImpl;
import psmjava.impl.JavaPackageImpl;
import br.unifacs.mda.plugin.exception.PimGeneratorException;
import br.unifacs.mda.plugin.psm.java.importation.ControllerJava;


/**
 * 
 * @author George Pacheco
 */
public class CodeGeneratorContent {

	private CodeGeneratorIF codeGenerator;
	private ControllerJava controllerJava;
	List<JavaClassImpl> classes = new ArrayList<JavaClassImpl>();
	List<JavaInterfaceImpl> interfaces = new ArrayList<JavaInterfaceImpl>();
	List<JavaEnumerationImpl> enumerations = new ArrayList<JavaEnumerationImpl>();
	List<JavaPackageImpl> packages = new ArrayList<JavaPackageImpl>();

	public CodeGeneratorContent(CodeGeneratorIF codeGenerator) {
		this.codeGenerator = codeGenerator;
	}

	public void start() throws PimGeneratorException, Exception {
		controllerJava = ControllerJava.getInstance(false);
		this.codeGenerator.init();

		JavaProject project = controllerJava.getProject();
		for (JavaElement elem : project.getElements()) {
			if (elem.getClass().equals(JavaPackageImpl.class)) {
				packages.add((JavaPackageImpl) elem);
			}
			this.findElements(elem);
		}

		this.codeGenerator.startPackage(packages);
		this.codeGenerator.startClass(classes);
		this.codeGenerator.startInterface(interfaces);
		this.codeGenerator.starEnumeration(enumerations);

	}

	public void findElements(JavaElement elem) {
		if (elem.getOwnedElement() != null) {
			for (JavaElement e : elem.getOwnedElement()) {
				this.findElements(e);
			}
		}
		if (elem.getClass().equals(JavaClassImpl.class)) {
			this.classes.add((JavaClassImpl) elem);
		} else if (elem.getClass().equals(JavaInterfaceImpl.class)) {
			this.interfaces.add((JavaInterfaceImpl) elem);
		} else if (elem.getClass().equals(JavaEnumerationImpl.class)) {
			this.enumerations.add((JavaEnumerationImpl) elem);
		}
	}
}
